********************************************************************************
*********************************Setup variables *******************************
********************************************************************************


*Globals

global pvar 	PB210 PB040* PB150 PE040 PL080 PY090*
global rvar 	RB050 RB090 RX010 RB210  // RB031 added in pooling dofile
global hvar 	HX060 HY050* HY060* HY070*

global ctr 		AT BE DE DK EL ES FI FR IE IT NL PT SE UK	
global t0 		10 11 12 13 14 15 16 17
global t1 		05 06 07
global t2 		08 09 10 
global t3 		11 12 13
global t4 		14 15 16 17
		
/*
Selected variables:

Personal data

PB210			Country of Birth
PB040			Personal cross-sectional weight
PB150			Gender
PE040			Highest level of Education (ISCED)
PL080			Months in unemployment
PY090*			Unemployment benefits


Personal register

RB031			Length of residency
RB050			Personal cross-sectional weight
RB090			Gender
RB210			Economic status
RX010			Age


Household data
	
HX060			Household type
HY050*			Family benefits
HY060*			Social exclusion benefits
HY070*			Housing benefits

*/


*Run dofile for pooling procedure of household/personal register/data (pdhr)

do "pooling.do"

*Select datasets and center variables per time frame

foreach t in t0 t1 t2 t3 t4 { 

global year $`t'

*global year 10 11 12 13 14 15 16 17

use "pdhr.dta", clear


*Reduce sample to selected countries and years

gen sample=.

foreach x in $ctr {
	replace sample=1 if country=="`x'"
}
	
foreach y in $year {
	replace sample=sample+1 if year==20`y'
}

keep if sample==2
drop sample

*Reduce sample to unemployed in working age (15-65 years old)

gen unemp=PL080>0 if PL080!=.

keep if RX010<=65 & RX010>=15 & unemp==1 

la var year "Year"

*Generate country-year clusters

egen ctnum = group(country)
labmask ctnum, values(country)
sort ctnum year
gen ctyear = ctnum*10+(year-2004)



**Binary variables for receipt of social benefits

*Unemployment benefits

*generate ub=PY090G_F>0 if PY090G_F>=0 & PY090G_F!=.

generate ub=1 if (PY090G_F>0 | PY090G_F>0) & PY090G_F!=. & PY090G_F!=.
replace ub=0 if (PY090G_F==0 | PY090G_F==0) 

*Family benefits
generate fb=1 if (HY050G_F>0 | HY050N_F>0) & HY050N_F!=. & HY050G_F!=.
replace fb=0 if (HY050G_F==0 | HY050N_F==0) 

*Social assistance benefits
generate ab=1 if (HY060G_F>0 | HY060N_F>0) & HY060N_F!=. & HY060G_F!=.
replace ab=0 if (HY060G_F==0 | HY060N_F==0) 

*Housing benefits
generate hb=1 if (HY070G_F>0 | HY070N_F>0) & HY070N_F!=. & HY070G_F!=.
replace hb=0 if (HY070G_F==0 | HY070N_F==0) 


*Unemployment

gen catue=1 if PL080<4
replace catue=2 if PL080>=4 & PL080<=9
replace catue=3 if PL080>9

la var catue "Months in unemployment"
la define catue_la 1 "< 4 months unemployed" 2 "4 -- 9 months unemployed (ref.)" 3 "> 9 months unemployed"
la values catue catue_la

tabulate catue, generate(ue)

la var ue1 "< 4 months unemployed"
la var ue2 "4 -- 9 months unemployed (ref.)"
la var ue3 "> 9 months unemployed"

*Migration variables

generat mig=0 if PB210=="LOC"
replace mig=1 if PB210=="EU"
replace mig=2 if PB210=="OTH"

la var mig "Country of birth"
la define mig_la 0 "Native-born" 1 "Europe" 2 "Other country"
la values mig mig_la

gen fob=mig>0 if mig!=.
la var fob "Foreign-born"
gen tfob=mig>0 if mig!=.
la var tfob "Foreign-born"

gen eur=mig==1
la var eur "European foreign-born"

gen tcn=mig==2
la var eur "Non-European foreign-born"

gen tres=((year-RB031)/5)+1

la var tres "Time of residency (5-year groups)"

*****Check! fob<->tres****
tab tres fob, mi
replace tres=. if fob!=1
replace tres=0 if fob==0
replace fob=. if tres==.

gen tr1=tres==1 if tres!=.
gen tr2=tres==2 if tres!=.
gen tr3=tres>2 if tres!=.
**************************

la var tr1 "< 5 years of residency"
la var tr2 "5 -- 9 years of residency"
la var tr3 "> 9 years of residency"

gen etr1=0 if fob!=.
replace etr1=1 if (tr1==1 | tr2==1) & mig==1
gen etr2=0 if fob!=.
replace etr2=1 if tr3==1 & mig==1

la var etr1 "Europe, $\leq$ 9 years of residency"
la var etr2 "Europe, $>$ 9 years of residency"


gen otr1=0 if fob!=.
replace otr1=1 if (tr1==1 | tr1==1) & mig==2
gen otr2=0 if fob!=.
replace otr2=1 if tr3==1 & mig==2

la var otr1 "Other, $\leq$ 9 years of residency"
la var otr2 "Other, $>$ 9 years of residency"

*Gender

gen female=PB150==2 if PB150!=.
la var female "Female"

*Age 

gen age=RX010 
la var age "Age"

*Education

destring PE040, gen(numed)

generat educ=1 if PE040<=2
replace educ=1 if PE040==100 | PE040==200
replace educ=2 if PE040==3 | PE040==4
replace educ=2 if PE040>=300 & PE040<500 
replace educ=3 if PE040==5 | PE040==6
replace educ=3 if PE040==500

gen hed=educ==3

la var hed "Tertiary Education"


*Household type

generat hhtype=1 if HX060==5
replace hhtype=2 if HX060==6 | HX060==7 | HX060==8
replace hhtype=3 if HX060==9
replace hhtype=4 if HX060==10 | HX060==11 | HX060==12 | HX060==13

tabulate hhtype, generate(type)
la var type1 "1 adult no children"
la var type2 "> 1 adult no children"
la var type3 "1 adult + children"
la var type4 "> 1 adult + children"

la var hhtype "Household type"
la define hhtype_la 1 "1 adult no children (ref.)" 2 "> 1 adult no children" 3 "1 adult + children" 4 "> 1 adult + children"
la values hhtype hhtype_la



*Add country-level variables

preserve
do "generosity.do"
do "control.do"
restore


merge m:1 country year using bengen.dta 
keep if _merge==3
drop _merge


merge m:1 country year using control.dta
keep if _merge==3
drop _merge


*/

*Save

save "`t'", replace

}



